package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;

public class BankApplication {

    public static ClientRegistrationListener listeners[] = new ClientRegistrationListener[3];

    private static Bank bank = new Bank(listeners);

    public static void modifyBank() throws NotEnoughFundsException {
        try {
            bank.clients[0].checkingAccount.deposit(16);
            bank.clients[0].checkingAccount.withdraw(215);
            bank.clients[0].savingAccount.withdraw(455);
        } catch (OverDraftLimitExceededException ov) {
            System.out.printf("Error! There are no proceeds of credit!\n%s\n", ov.toString());

        }
    }


    public static void printBalance() {

        int length = bank.clients.length;
        for (int i = 0; i < length; i++) {
            if (bank.clients[i] == null) {
                break;
            }

            if (bank.clients[i].savingAccount != null) {
                System.out.printf("Client #%d || FIO:  %s || Saving Balance: %.2f\n", i + 1, bank.clients[i].getClientSalutation(), bank.clients[i].savingAccount.getBalance());
            }
            if (bank.clients[i].checkingAccount != null) {
                System.out.printf("Client #%d || FIO:  %s || Checking Balance: %.2f\n", i + 1, bank.clients[i].getClientSalutation(), bank.clients[i].checkingAccount.getBalance());
            }

        }
    }

    public static void main(String[] args) throws BankException {

        listeners[0] = new Bank.PrintClientListener();
        listeners[1] = new Bank.EmailNotificationListener();
        listeners[2] = new Bank.DebugListener();

        BankService.addClient(bank, new Client("Kolobkova Ira", Gender.Female, 12346.56, 45621.2, 4569.2));

        BankService.addClient(bank, new Client("Ivanov Petr", Gender.Male, 456.23));

        BankService.addClient(bank, new Client("Petrova Svetlana", Gender.Female, 7.0));

        BankService.addClient(bank, new Client("Toropov Ivan", Gender.Male, 12.5, 19.4, 26.3));

        System.out.printf("\n------------------------------------------HashCode-----------------------------------\n");

        for (int i = 0; i < bank.clients.length; i++) {
            if (bank.clients[i] == null)
                break;
            System.out.printf("Client# %d: %d\n", i + 1, bank.clients[i].hashCode());
        }
        System.out.printf("\n------------------------------------------Equals-----------------------------------\n");
        System.out.printf("Client #1 equals to client #2 ?\n Answer: %s\n", bank.clients[0].equals(bank.clients[1]));
        System.out.printf("Client #3 equals to client #4 ?\n Answer: %s\n", bank.clients[2].equals(bank.clients[3]));

        modifyBank();

        System.out.printf("\n------------------------------------------Modify-------------------------------------\n");

        printBalance();
    }

}